package com.sinoglobal.wallet.api;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.sinoglobal.wallet.app.SinoConstans;
import com.sinoglobal.wallet.app.SinoValueManager;
import com.sinoglobal.wallet.entity.W_AlipayEntiy;
import com.sinoglobal.wallet.entity.W_BalanceEntity;
import com.sinoglobal.wallet.entity.W_CouponDetail;
import com.sinoglobal.wallet.entity.W_CouponInstrutionEntity;
import com.sinoglobal.wallet.entity.W_CouponListEntity;
import com.sinoglobal.wallet.entity.W_DynamicKeyEntity;
import com.sinoglobal.wallet.entity.W_HomeBalanceEntity;
import com.sinoglobal.wallet.entity.W_OrderStateEntiy;
import com.sinoglobal.wallet.entity.W_RechargeRecordItemEntity;
import com.sinoglobal.wallet.entity.W_RsListVo;
import com.sinoglobal.wallet.entity.W_RsVo;
import com.sinoglobal.wallet.entity.W_SinoBaseEntity;
import com.sinoglobal.wallet.entity.W_UpayEntiy;
import com.sinoglobal.wallet.entity.W_VerifyPassNumVo;
import com.sinoglobal.wallet.entity.W_VerifyPassVo;
import com.sinoglobal.wallet.entity.W_WechatEntiy;
import com.sinoglobal.wallet.entity.W_WithdrawalMoneyEntity;
import com.sinoglobal.wallet.entity.W_WithdrawalSuccessEntity;
import com.sinoglobal.wallet.entity.W_WithdrawlInstrutionEntiy;
import com.sinoglobal.wallet.http.ConnectionUtil;
import com.sinoglobal.wallet.http.W_ConnectionUtil;
import com.sinoglobal.wallet.util.LogUtils;

/**
 * @title:RemoteImpl.java
 * @package com.sinoglobal.sinobase.api
 * @description: 请求接口封装
 */
public class RemoteImpl implements Remote {

	private static RemoteImpl remoteImpl;

	private RemoteImpl() {

	}

	static {
		remoteImpl = new RemoteImpl();
	}

	public static RemoteImpl getInstance() {
		return remoteImpl;
	}

	// 主页数据
	@Override
	public W_HomeBalanceEntity getBalance(Context context) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", SinoValueManager.getUserId(context));
		params.put("productCode", SinoValueManager.getProductID(context));
		String json = W_ConnectionUtil.post(params, "/wallet/getBalance?json=", SinoConstans.SERVER_FLAG);// SinoConstans.TEST120_FLAG);
		LogUtils.v("getBalance" + json);
		return JSON.parseObject(json, W_HomeBalanceEntity.class);
	}

	// 提现请求
	@Override
	public W_WithdrawalSuccessEntity getTiXian(Context context, String password, String amount, String tradeSn, String modelid, String payType, String userbcid, String businessTypeKey, String id, String key) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", SinoValueManager.getUserId(context));
		params.put("productCode", SinoValueManager.getProductID(context));
		params.put("payPassWord", password);
		params.put("amount", amount);
		params.put("tradeSn", tradeSn);
		params.put("modelid", modelid);
		params.put("payType", payType);
		params.put("userbcid", userbcid);
		params.put("num", "0");// 后台更新num=0；
		params.put("businessTypekey", businessTypeKey);
		String json = W_ConnectionUtil.postEncrption(params, "/tixian/getMoney?json=", SinoConstans.SERVER_FLAG, id, key);// SinoConstans.TEST120_FLAG);
		LogUtils.v("getTiXian" + json);
		return JSON.parseObject(json, W_WithdrawalSuccessEntity.class);
	}

	// 提现记录
	@Override
	public W_WithdrawalMoneyEntity getDrawMoneyLog(Context context, String page, String count) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", SinoValueManager.getUserId(context));
		params.put("productCode", SinoValueManager.getProductID(context));
		params.put("page", page);
		params.put("count", count);
		String json = W_ConnectionUtil.post(params, "/money/getDrawMoneyLog?json=", SinoConstans.SERVER_FLAG);// SinoConstans.TEST120_FLAG);
		LogUtils.v("getDrawMoneyLog" + json);
		return JSON.parseObject(json, W_WithdrawalMoneyEntity.class);
	}

	@Override
	public W_WithdrawlInstrutionEntiy getWithDrawlInstrution() throws Exception {
		String json = W_ConnectionUtil.postGetKey("/tixian/getconf", SinoConstans.SERVER_FLAG);
		return JSON.parseObject(json, W_WithdrawlInstrutionEntiy.class);
	}

	// 获取秘钥
	@Override
	public W_DynamicKeyEntity getDynamicKey() throws Exception {
		String json = W_ConnectionUtil.postGetKey("/encrypt/getkey", SinoConstans.SERVER_Encryption_FLAG);// SinoConstans.TEST120_FLAG);
		return JSON.parseObject(json, W_DynamicKeyEntity.class);
	}

	// 余额记录
	@Override
	public W_BalanceEntity getDetail(Context context, String page, String amountType, String count) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", SinoValueManager.getUserId(context));
		params.put("page", page);
		params.put("count", count);
		params.put("amountType", amountType);
		String json = W_ConnectionUtil.post(params, "/balance/getDetail?json=", SinoConstans.SERVER_FLAG);// SinoConstans.TEST120_FLAG);
		LogUtils.v("getDrawMoneyLog" + json);
		return JSON.parseObject(json, W_BalanceEntity.class);
	}

	// 获取手机验证码接口
	@Override
	public W_RsVo getCode(Context context) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("productCode", SinoValueManager.getProductID(context));
		params.put("userId", SinoValueManager.getUserId(context));
		String json = W_ConnectionUtil.post(params, "/pay/getCode?json=", SinoConstans.SERVER_FLAG);
		return JSON.parseObject(json, W_RsVo.class);
	}

	// 验证手机验证码接口
	@Override
	public W_RsVo verifyCode(Context context, String captcha) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("productCode", SinoValueManager.getProductID(context));
		params.put("userId", SinoValueManager.getUserId(context));
		params.put("captcha", captcha);
		String json = W_ConnectionUtil.post(params, "/pay/checkCode?json=", SinoConstans.SERVER_FLAG);
		return JSON.parseObject(json, W_RsVo.class);
	}

	// 设置支付密码
	@Override
	public W_SinoBaseEntity settingPassWord(Context context, String paypassword, String id, String key) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("productCode", SinoValueManager.getProductID(context));
		params.put("userId", SinoValueManager.getUserId(context));
		params.put("payPassword", paypassword);
		String json = W_ConnectionUtil.postEncrption(params, "/pay/setPassword?json=", SinoConstans.SERVER_FLAG, id, key);
		return JSON.parseObject(json, W_SinoBaseEntity.class);
	}

	// 验证支付密码
	@Override
	public W_VerifyPassVo verifyPassWord(Context context, String payPassword, String id, String key) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("productCode", SinoValueManager.getProductID(context));
		params.put("userId", SinoValueManager.getUserId(context));
		params.put("payPassword", payPassword);
		String json = W_ConnectionUtil.postEncrption(params, "/pay/checkPassword?json=", SinoConstans.SERVER_FLAG, id, key);
		return JSON.parseObject(json, W_VerifyPassVo.class);
	}

	// 获取输入错误密码的次数
	@Override
	public W_VerifyPassNumVo getUnPassWordNum(Context context) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("productCode", SinoValueManager.getProductID(context));
		params.put("userId", SinoValueManager.getUserId(context));
		String json = W_ConnectionUtil.post(params, "/wallet/getErrNum?json=", SinoConstans.SERVER_FLAG);
		return JSON.parseObject(json, W_VerifyPassNumVo.class);
	}

	// 修改支付密码
	@Override
	public W_SinoBaseEntity updatePassWord(Context context, String payPassword) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("productCode", SinoValueManager.getProductID(context));
		params.put("userId", SinoValueManager.getUserId(context));
		params.put("payPassword", payPassword);
		String json = W_ConnectionUtil.post(params, "/pay/setPassword?json=", SinoConstans.SERVER_FLAG);
		return JSON.parseObject(json, W_SinoBaseEntity.class);
	}

	// 是否设置了支付密码
	@Override
	public W_RsVo getSelectPassWord(Context context) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("productCode", SinoValueManager.getProductID(context));
		params.put("userId", SinoValueManager.getUserId(context));
		String json = W_ConnectionUtil.post(params, "/pay/passwordIsOK?json=", SinoConstans.SERVER_FLAG);
		return JSON.parseObject(json, W_RsVo.class);
	}

	// 获取用户银行卡列表
	@Override
	public W_RsListVo getBankList(Context context) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("productCode", SinoValueManager.getProductID(context));
		params.put("userId", SinoValueManager.getUserId(context));
		String json = W_ConnectionUtil.post(params, "/bankcardlist/getBankCardList?json=", SinoConstans.SERVER_FLAG);
		return JSON.parseObject(json, W_RsListVo.class);
	}

	// 添加用户银行卡
	@Override
	public W_SinoBaseEntity getAddBank(Context context, String bankId, String bankCardSN, String bankCardUser) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("productCode", SinoValueManager.getProductID(context));
		params.put("userId", SinoValueManager.getUserId(context));
		params.put("bankId", bankId);
		params.put("bankCardSN", bankCardSN);
		params.put("bankCardUser", bankCardUser);
		String json = W_ConnectionUtil.post(params, "/wallet/addBankCard?json=", SinoConstans.SERVER_FLAG);
		return JSON.parseObject(json, W_SinoBaseEntity.class);
	}

	// 删除用户银行卡
	@Override
	public W_SinoBaseEntity getDeleteBank(Context context, String userbcid, String status) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("productCode", SinoValueManager.getProductID(context));
		params.put("userbcid", userbcid);
		params.put("status", status);
		String json = W_ConnectionUtil.post(params, "/bank/CancelBound?json=", SinoConstans.SERVER_FLAG);
		return JSON.parseObject(json, W_SinoBaseEntity.class);
	}

	// 获取银行卡详情
	/*
	 * @Override public BankXqVo getBankXQ(Context context, String userbcid)
	 * throws Exception { Map<String, Object> params = new HashMap<String,
	 * Object>(); params.put("productCode",
	 * SinoValueManager.getProductID(context)); params.put("userbcid",
	 * userbcid); String json = ConnectionUtil.post(params,
	 * "/bank/getBankCardDetail?json=", SinoConstans.SERVER_FLAG); return
	 * JSON.parseObject(json, BankXqVo.class); }
	 */
	/**
	 * 二期的接口如下
	 */
	// 获取
	@Override
	public W_AlipayEntiy getAlipay(Context context, String orderSn, String amount, String goodDepice, String goodName, String modelId, String paySource) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("productCode", SinoValueManager.getProductID(context));
		params.put("userId", SinoValueManager.getUserId(context));
		params.put("amount", amount);
		params.put("orderSn", orderSn);
		params.put("goodDepice", goodDepice);
		params.put("goodName", goodName);
		params.put("modelId", modelId);
		params.put("paySource", paySource);
		String json = W_ConnectionUtil.post(params, "/payment/alipay?json=", SinoConstans.SERVER_FLAG);
		return JSON.parseObject(json, W_AlipayEntiy.class);
	}

	@Override
	public W_WechatEntiy getWeChat(Context context, String orderSn, String amount, String goodDepice, String goodName, String modelId, String paySource) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("productCode", SinoValueManager.getProductID(context));
		params.put("userId", SinoValueManager.getUserId(context));
		params.put("orderSn", orderSn);
		params.put("amount", amount);
		params.put("goodDepice", goodDepice);
		params.put("goodName", goodName);
		params.put("modelId", modelId);
		params.put("paySource", paySource);
		String json = W_ConnectionUtil.post(params, "/payment/wechatpay?json=", SinoConstans.SERVER_FLAG);
		return JSON.parseObject(json, W_WechatEntiy.class);
	}

	@Override
	public W_UpayEntiy getUpay(Context context, String orderSn, String amount, String goodDepice, String goodName, String modelId, String paySource) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("productCode", SinoValueManager.getProductID(context));
		params.put("userId", SinoValueManager.getUserId(context));
		params.put("orderSn", orderSn);
		params.put("amount", amount);
		params.put("goodDepice", goodDepice);
		params.put("goodName", goodName);
		params.put("modelId", modelId);
		params.put("paySource", paySource);
		String json = W_ConnectionUtil.post(params, "/payment/ufpay?json=", SinoConstans.SERVER_FLAG);
		return JSON.parseObject(json, W_UpayEntiy.class);
	}

	@Override
	public W_OrderStateEntiy getOrderState(Context context, String orderSn) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("orderSn", orderSn);
		String json = W_ConnectionUtil.post(params, "/recharge/getOrderStatus?json=", SinoConstans.SERVER_FLAG);
		return JSON.parseObject(json, W_OrderStateEntiy.class);
	}

	// 充值记录
	@Override
	public W_RechargeRecordItemEntity getChargeLog(Context context, String page, String count) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", SinoValueManager.getUserId(context));
		params.put("productCode", SinoValueManager.getProductID(context));
		params.put("page", page);
		params.put("count", count);
		String json = W_ConnectionUtil.post(params, "/money/getChargeLog?json=", SinoConstans.SERVER_FLAG);
		LogUtils.v("getChargeLog" + json);
		return JSON.parseObject(json, W_RechargeRecordItemEntity.class);
	}

	// 优惠券列表
	@Override
	public W_CouponListEntity getCouponList(Context context, int currentPage, int useState, int count, String couponType) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		// params.put("userId",SinoValueManager.getUserId(context));
		params.put("userId", SinoValueManager.getUserId(context));
		params.put("useState", useState);
		params.put("currentPage", currentPage);
		params.put("couponType", couponType);
		params.put("pageSize", count);
		String json = ConnectionUtil.postNew(params, "couponClaim/queryCouponByUser.do", SinoConstans.SERVER_Coupon_FLAG);
		// LogUtils.v("getCouponList" + json);
		return JSON.parseObject(json, W_CouponListEntity.class);
	}

	// 优惠券详情
	@Override
	public W_CouponDetail getCouponDetail(int claimId) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("claimId", claimId);
		String json = ConnectionUtil.postNew(params, "couponClaim/queryCouponUseByClaimId.do", SinoConstans.SERVER_Coupon_FLAG);
		return JSON.parseObject(json, W_CouponDetail.class);
	}
	
	// 优惠券说明
	@Override
	public W_CouponInstrutionEntity getCouponInstrution() throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		String json = ConnectionUtil.postNew(params, "coupon/queryCouponDesc.do", SinoConstans.SERVER_Coupon_FLAG);
		return JSON.parseObject(json, W_CouponInstrutionEntity.class);
	}

				
}
